import request from '@/utils/request'

export const generateImage = async (data) => {
  try {
    console.log('发送图片生成请求:', data)
    const response = await request({
      url: '/api/media-resources/generate-image',
      method: 'post',
      data
    })
    
    console.log('收到图片生成响应:', response)
    
    // 解析返回数据
    if (response && response.data && response.data.success) {
      return {
        success: true,
        data: response.data.data
      }
    } else if (response && response.data) {
      throw new Error(response.data.message || '图片生成失败')
    } else {
      throw new Error('未获取到有效响应')
    }
  } catch (error) {
    console.error('图片生成API调用失败:', error)
    throw error
  }
}

export const fetchImages = async () => {
  try {
    const response = await request({
      url: '/api/media-resources/images',
      method: 'get'
    })
    
    if (response && response.data && response.data.success) {
      return response.data.data
    } else {
      throw new Error('获取图片列表失败')
    }
  } catch (error) {
    console.error('获取图片列表失败:', error)
    throw error
  }
} 